<template>
	<view class="wrap-fixed-top"
		:style="'background:' + bgColor + ';'"
		:class="isFinishCalculation ? 'fixed' : 'sticky'"
	>
		<!--  -->
		<image
			v-if="bgImgType > -1"
			class="fixed-top-bg" 
			mode="widthFix" 
			:src="onGetBgImg()"
		/>
		
		<!-- 导航栏 -->
		<view class="wrap-navigation-bar"
			v-if="isShowTop"
			:style="'padding-top: ' + statusHeight + 'px;'"
		>
			<view class="navigation-bar">
				<view class="navigation-back" 
					@click.stop="onNavigationLeft"
					v-if="isHaveBack"
				>
					<image
						class="img"
						mode="heightFix"
						:src="onGetBackImg()" 
					/>
				</view>
		
				<view class="navigation-title"
					:class="['black', 'white'][showType]"
				>
					{{ navigationTitle }}
				</view>
			</view>
		</view>
		
		<view class="wrap-slot">
			<slot></slot>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			/**
			 * 背景颜色
			 */
			bgColor: {
				type: String,
				default: "transparent",
			},
			/**
			 * 背景图片 -1是没有图片 0:照片顶部图片
			 */
			bgImgType: {
				type: Number,
				default: -1,
			},
			/**
			 * 是否带导航栏
			 */
			isShowTop: {
				type: Boolean,
				default: true
			},
			/**
			 * 是否带返回
			 */
			isHaveBack: {
				type: Boolean,
				default: true,
			},
			/**
			 * 返回图片的样式 0:黑色> 1:白色> 2:黑色X
			 */
			backType: {
				type: Number,
				default: 0,
			},
			/**
			 * 显示文字颜色 0:黑色 1:白色
			 */
			showType: {
				type: Number,
				default: 0,
			},
			/**
			 * 标题
			 */
			navigationTitle: {
				type: String,
				default: "",
			},
		},
		data() {
			return {
				statusHeight: uni.getWindowInfo().statusBarHeight,
				isFinishCalculation: false, //是否完成计算
			}
		},
		mounted() {
			this.$nextTick(() => {
				this.onCalculationHeight();
			})
		},
		methods: {
			/**
			 * 获取背景图片
			 */
			onGetBgImg() {
				var bgImgList = [
					"store/bg.png", //商家入驻顶部图片
					"goods/goods_top.png", //商品顶部图片
				];
				
				var bgImg = bgImgList[this.bgImgType];
				return this.$staticImagePath(bgImg);
			},
			/**
			 * 获取返回的图片
			 */
			onGetBackImg() {
				var imgList = [
					"store/navigation_left.png",
				];
				
				var img = imgList[this.backType];
				return this.$staticImagePath(img);
			},
			/**
			 * 返回
			 */
			onNavigationLeft() {
				let pages = getCurrentPages();
				if (pages.length > 1) {
					uni.navigateBack();
				} else {
					uni.switchTab({
						url: "/pages/index"
					})
				}
			},
			/**
			 * 计算高度
			 */
			onCalculationHeight() {
				let query = uni.createSelectorQuery().in(this);
				query.select(".wrap-fixed-top").boundingClientRect(res => {
					if (res && res.hasOwnProperty("height") && res.height > 0) {
						this.$emit("onTotalHeight", res.height);
						this.isFinishCalculation = true;
					}
				})
				query.exec();
			},
		}
	}
</script>

<style lang="scss" scoped>
	.fixed {
		position: fixed;
	}
	.sticky {
		position: sticky;
	}
	.wrap-fixed-top {
		left: 0;
		top: 0;
		width: 100%;
		z-index: 99;
		overflow: hidden;
		
		.fixed-top-bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			z-index: 100;
		}
		
		.wrap-navigation-bar {
			width: 100%;
			overflow: hidden;
			position: relative;
			z-index: 101;
			
			.navigation-bar {
				width: 100%;
				height: 45px;
				overflow: hidden;
				position: relative;
				z-index: 101;
			
				.navigation-back {
					position: absolute;
					left: 0;
					top: 0;
					width: 45px;
					height: 45px;
					z-index: 102;
					display: flex;
					align-items: center;
					justify-content: center;
			
					.img {
						width: auto;
						height: 34rpx;
					}
				}
			
				.navigation-title {
					position: absolute;
					left: 50px;
					top: 0;
					width: calc(100% - 100px);
					height: 45px;
					z-index: 102;
					overflow: hidden;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 36rpx;
				}
			}
		}
	}
	
	.wrap-slot {
		width: 100%;
		overflow: hidden;
		position: relative;
		z-index: 102;
	}
</style>